package com.example.admin.controller.positions;

import com.example.airobotpositions.dao.AddPositionsDto;
import com.example.airobotpositions.dao.PositionQueryDto;
import com.example.airobotpositions.dao.UpdatePositionDto;
import com.example.airobotpositions.service.IInterviewPositionsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 面试职位信息表 前端控制器
 * </p>
 *
 * @author 沈骜嘉
 * @since 2025-04-15
 */
@Api(tags = "职位管理接口")
@RestController
@RequestMapping("/api/positions")
public class InterviewPositionsController {

    @Autowired
    private IInterviewPositionsService interviewPositionsService;

    @PostMapping("/add")
    @ApiOperation("添加职位")
    public ResponseEntity<Map<String, Object>> addPosition(@Validated @RequestBody AddPositionsDto dto) {
        Integer positionId = interviewPositionsService.addPosition(dto);
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "添加成功");
        result.put("data", positionId);
        
        return ResponseEntity.ok(result);
    }

    @PostMapping("/query")
    @ApiOperation("查询职位信息")
    public ResponseEntity<Map<String, Object>> queryPositions(@RequestBody PositionQueryDto queryDto) {
        Object result = interviewPositionsService.queryPositions(queryDto);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("msg", "查询成功");
        response.put("data", result);
        
        return ResponseEntity.ok(response);
    }
    
    @PostMapping("/update")
    @ApiOperation("修改职位信息")
    public ResponseEntity<Map<String, Object>> updatePosition(@Validated @RequestBody UpdatePositionDto dto) {
        Boolean success = interviewPositionsService.updatePosition(dto);
        
        Map<String, Object> response = new HashMap<>();
        if (success) {
            response.put("code", 200);
            response.put("msg", "修改成功");
        } else {
            response.put("code", 500);
            response.put("msg", "修改失败，职位不存在");
        }
        
        return ResponseEntity.ok(response);
    }
    
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除职位信息")
    public ResponseEntity<Map<String, Object>> deletePosition(@PathVariable Integer id) {
        Boolean success = interviewPositionsService.deletePosition(id);
        
        Map<String, Object> response = new HashMap<>();
        if (success) {
            response.put("code", 200);
            response.put("msg", "删除成功");
        } else {
            response.put("code", 500);
            response.put("msg", "删除失败，职位不存在");
        }
        
        return ResponseEntity.ok(response);
    }
}
